package com.eric.service.impl;

import cn.dev33.satoken.util.SaResult;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eric.domain.dto.EquipmentDTO;
import com.eric.domain.dto.MaterialDTO;
import com.eric.domain.po.Equipment;
import com.eric.domain.po.Material;
import com.eric.mapper.MaterialMapper;
import com.eric.service.IMaterialService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 材料表 服务实现类
 * </p>
 *
 * @author eric
 * @since 2024-12-21
 */
@Service
public class MaterialServiceImpl extends MPJBaseServiceImpl<MaterialMapper, Material> implements IMaterialService {

    @Override
    public SaResult getAllByPageService(Integer current, Integer pageSize) {
        if (current==null){
            current=1;
        }
        if (pageSize==null||pageSize<=10){
            pageSize=10;
        }

        Page<MaterialDTO> page=new Page<>(current,pageSize);
        page.setSize(pageSize);
        page.setCurrent(current);
        page.setOptimizeCountSql(false);


        MPJLambdaWrapper<Material> wrapper =new MPJLambdaWrapper<Material>()
                .selectAll(Material.class)
                .eq(Material::getDeleted,0)
                .eq(Material::getStatus,1)
                .groupBy("create_time")
                .orderByDesc("create_time");

        Page<MaterialDTO> listPage = selectJoinListPage(page,MaterialDTO.class, wrapper);
        return SaResult.data(listPage.getRecords());
    }

    @Override
    public SaResult getOneByIdService(Integer id) {
        MPJLambdaWrapper<Material>wrapper=new MPJLambdaWrapper<Material>()
                .selectAll(Material.class)
                .eq(Material::getId,id)
                .eq(Material::getDeleted,0)
                .eq(Material::getStatus,1);
        return  SaResult.data(selectJoinOne(MaterialDTO.class, wrapper)) ;
    }
}
